package com.yf.course.modules.course.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.ability.log.annon.LogInject;
import com.yf.ability.log.enums.LogType;
import com.yf.ability.upload.providers.local.service.LocalUpService;
import com.yf.base.api.annon.DataProtect;
import com.yf.base.api.api.ApiRest;
import com.yf.base.api.api.controller.BaseController;
import com.yf.base.api.api.dto.BaseIdReqDTO;
import com.yf.base.api.api.dto.BaseIdsReqDTO;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.base.utils.BeanMapper;
import com.yf.course.modules.course.dto.CourseFileDTO;
import com.yf.course.modules.course.dto.CourseRefFileDTO;
import com.yf.course.modules.course.dto.ext.CourseRefDirExtDTO;
import com.yf.course.modules.course.dto.request.CourseFileFetchReqDTO;
import com.yf.course.modules.course.dto.request.CourseFileReqDTO;
import com.yf.course.modules.course.entity.CourseFile;
import com.yf.course.modules.course.service.CourseFileService;
import com.yf.course.modules.course.service.CourseRefFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
* <p>
* 课件信息控制器
* </p>
*
* @author 聪明笨狗
* @since 2020-12-17 11:28
*/
@Api(tags={"课件信息"})
@RestController
@RequestMapping("/api/course/file")
public class CourseFileController extends BaseController {

    @Autowired
    private CourseFileService baseService;

    @Autowired
    private LocalUpService uploadService;

    @Autowired
    private CourseRefFileService courseRefFileService;

    /**
    * 添加或修改
    * @param reqDTO
    * @return
    */
//    @RequiresPermissions(value = {"course:file:add","course:file:update"}, logical = Logical.OR)
    @LogInject(title="添加或修改课件", logType = LogType.COURSE)
    @DataProtect(clazz = CourseFile.class, update = true)
    @ApiOperation(value = "添加或修改")
    @PostMapping("/save")
    public ApiRest<CourseFileDTO> save(@RequestBody CourseFileDTO reqDTO) {
        CourseFileDTO respDTO = baseService.save(reqDTO);
        return super.success(respDTO);
    }


    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @LogInject(title="删除课件", logType = LogType.COURSE)
    @RequiresPermissions(value={"course:file:delete"})
    @DataProtect(clazz = CourseFile.class, delete = true)
    @ApiOperation(value = "批量删除")
    @PostMapping("/delete")
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        //WARNING:由于实际存储空间可能不足，做本地文件的实际删除操作，如果空间充足，建议不做实际文件删除操作
        //查询文件获取file_url
        //List<CourseFile> list = baseService.listByIds(reqDTO.getIds());
        //List<String> fileUrls = list.stream().map(CourseFile::getFileUrl).collect(Collectors.toList());
        ////实际删除文件
        //uploadService.delete(fileUrls);
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
    * 查找详情
    * @param reqDTO
    * @return
    */
    @RequiresPermissions(value = {"course:file:add","course:file:update"}, logical = Logical.OR)
    @ApiOperation(value = "查找详情")
    @PostMapping("/detail")
    public ApiRest<CourseFileDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        CourseFile entity = baseService.getById(reqDTO.getId());
        CourseFileDTO dto = new CourseFileDTO();
        BeanMapper.copy(entity, dto);
        return super.success(dto);
    }

    /**
     * 校验课件
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "校验课件", notes = "主要用于检查课程是否关联了课件，否则不让查看播放")
    @PostMapping("/check-file")
    public ApiRest<Boolean> checkFile(@RequestBody CourseRefFileDTO reqDTO) {
        Boolean check = courseRefFileService.checkFile(reqDTO);
        return super.success(check);
    }

    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
//    @RequiresPermissions("course:file:list")
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public ApiRest<IPage<CourseFileDTO>> paging(@RequestBody PagingReqDTO<CourseFileReqDTO> reqDTO) {

        //分页查询并转换
        IPage<CourseFileDTO> page = baseService.paging(reqDTO);
        return super.success(page);
    }


    /**
     * 根据分类获取课件目录
     * @param reqDTO
     * @return
     */
    @RequiresPermissions("course:file:list")
    @ApiOperation(value = "根据分类获取课件目录", notes = "主要用于检查课程是否关联了课件，否则不让查看播放")
    @PostMapping("/fetch-file")
    public ApiRest<List<CourseRefDirExtDTO>> fetchFile(@RequestBody CourseFileFetchReqDTO reqDTO) {
        List<CourseRefDirExtDTO> respDTO = baseService.fetchByCatId(reqDTO);
        return super.success(respDTO);
    }



}
